package com.atguigu.controller;

import com.atguigu.entity.UserInfo;
import com.atguigu.entity.vo.UserFollowVo;
import com.atguigu.result.Result;
import com.atguigu.service.UserFollowService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Author:hx
 * @DateTime:2022/9/30 23:13
 * @Description:
 */
@RestController
@RequestMapping("/userFollow")
public class UserFollowController {
    @DubboReference
    private UserFollowService userFollowService;

    @RequestMapping("/auth/follow/{houseId}")
    public Result addFollow(@PathVariable("houseId") Long houseId, HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("USER");
        Long userId = userInfo.getId();
        userFollowService.addFollow(userId,houseId);
        return Result.ok();
    }

    @RequestMapping("/auth/cancelFollow/{houseId}")
    public Result cancelFollow(@PathVariable Long houseId,HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("USER");
        userFollowService.cancelFollow(userInfo.getId(),houseId);
        return Result.ok();
    }

    @RequestMapping("/auth/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize,HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("USER");
        PageInfo<UserFollowVo> page = userFollowService.findUserFollowVoPage(pageNum,pageSize,userInfo.getId());
        return Result.ok(page);
    }

    @RequestMapping("/auth/cancelFollowById/{id}")
    public Result cancelFollowById(@PathVariable Long id){
        userFollowService.cancelFollowById(id);
        return Result.ok();
    }
}
